import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Deque;
import java.util.List;

public class Demo {
    List<List<Integer>> result = new ArrayList<>();
    Deque<Integer> sub = new ArrayDeque<>();
    public  List<List<Integer>> combine(int n, int k){
        dfs(n,k,1);
        return result;
    }
    private void dfs(int n, int k, int cur){
        if(sub.size() == k){
            result.add(new ArrayList<>(sub));
            return;
        }
        for (int i = cur; i <= n; i++){
            sub.addLast(i);
            dfs(n, k, i + 1);
            sub.removeLast();
        }
    }

    public static void main(String[] args) {
        Demo solution = new Demo();
        int n = 4;
        int k = 2;
        List<List<Integer>> res = solution.combine(n, k);
        System.out.println(res);
    }
}
